<?php

namespace App\Observers;

use App\Facades\User\UserCacheFacade;
use App\Models\User\User;
use Ramsey\Uuid\Uuid;

class UserObserver
{
    public function creating(User $user)
    {
        $user->user_id = Uuid::uuid1()->toString();
    }

    /**
     * Handle the User "created" event.
     *
     * @param User $user
     * @return void
     */
    public function created(User $user)
    {
        UserCacheFacade::cache($user);
    }

    /**
     * Handle the User "updated" event.
     *
     * @param User $user
     * @return void
     */
    public function updated(User $user)
    {
        UserCacheFacade::cache($user);
    }

    public function saved(User $user)
    {
        UserCacheFacade::cache($user);
    }

    /**
     * Handle the User "deleted" event.
     *
     * @param User $user
     * @return void
     */
    public function deleted(User $user)
    {
        UserCacheFacade::forget($user->getUserId());
    }

    /**
     * Handle the User "restored" event.
     *
     * @param User $user
     * @return void
     */
    public function restored(User $user)
    {
        UserCacheFacade::cache($user);
    }

    /**
     * Handle the User "force deleted" event.
     *
     * @param User $user
     * @return void
     */
    public function forceDeleted(User $user)
    {
        UserCacheFacade::forget($user->getUserId());
    }
}
